<?php

class JobsDocumentsController extends Controller{
    public $layout='//layouts/sp';
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            array(
                'application.filters.RequireLoginFilter',
            ),
        );
    }
    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions				
             'users'=>array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }
    public function actionIndex($jobid){
        $job=Jobs::model()->findByPk($jobid);
        if($job==NULL) throw new CHttpException ('The requested page can not found',404);
        $this->render('index',array('job'=>$job));
    }
    public function actionCreate($jobid){
        $model=new JobsDocuments;
        $model->status=-1;
      
            $model->attributes=$_POST;
            
            if (isset($_FILES['file']))  // file was send from browser
            {
                
                if ($_FILES['file']['error'] == UPLOAD_ERR_OK)  // no error
                {
                    $filename = $_FILES['file']['name']; // file name 
                    $name=getFileBodyName($filename);
                    $ext=getFileExt($filename);
                    $newname=$name.time().$ext;
                    move_uploaded_file($_FILES['file']['tmp_name'], 'documents/'.$newname);
                    $model->filename=$newname;
                    // main action -- move uploaded file to $upload_dir 
                    $result = 'OK';
                }
                elseif ($_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE)
                    $result_msg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
                else 
                    $result_msg = 'Unknown error';

            }
            
            $model->jobid=$jobid;
            if($model->save()){
                echo json_encode(array(
                    'success'=>true,
                    'msg'=>'Save successful'
                ));
            }else{
                echo json_encode(array(
                    'success'=>false,
                    'errors'=>$model->errors
                ));
            }
        
    }
    public function actionUpdate($id){
        $model=JobsDocuments::model()->findByPk($id);
        
       
            $model->attributes=$_POST;
           
            if($model->save()){
                echo json_encode(array(
                    'success'=>true,
                    'msg'=>'Save successful'
                ));
            }else{
                echo json_encode(array(
                    'success'=>false,
                    'errors'=>$model->errors
                ));
            }
        
    }
    public function actionDownload($id){
        $doc=JobsDocuments::model()->findByPk($id);
        if($doc!=NULL)
            Yii::app()->fileHelper->forceDownload('documents/'.$doc->filename);
    }
    public function actionDelete($id){
        $doc=JobsDocuments::model()->findByPk($id);
        if($doc!=NULL) $doc->delete();
    }
    
    public function actionList($jobid,$page=0,$sort=NULL){
        if($page>0) $page--;
        $order=CJSON::decode($sort);

        $data=new CActiveDataProvider('JobsDocuments',array(
            'criteria'=>array(
                'condition'=>'jobid=:jobid',
                'params'=>array(
                    'jobid'=>$jobid,                     
                ),
                'order'=>isset($order)?$order[0]['property'].' '.$order[0]['direction']:NULL
            ),
            'pagination'=>array(
                'pageSize'=>20,
                'currentPage'=>$page
            )
        ));

        if($data!=NULL && count($data->getData())>0){
            $rs=array();
            $rs['totalCount']=$data->getTotalItemCount();
            foreach($data->getData() as $i=>$item){

                $rs['data'][$i]=$item->getAttributes();

            }
            echo json_encode($rs);
        }
        Yii::app()->end();
    }
}
?>
